<?php
class Application_Form_RegisterForm extends Zend_Form
{
	private $subForms = array();

	
	public function __construct( $_options = array() ){

        parent::__construct();

		$this->setMethod('post');
		if ( isset($_options['action']) ){
             $this->setAction( $_options['action'] ) ;
		}
		else{
				 $this->setAction( '/users/register/save' ) ;
		}

		$this->setAttrib( 'id', 'registerFrm' ) ;

		$this->subForms = array(
			'farmacist' => array(
				'account' => array( 
					'legend' => 'Adauga datele contului', 
					'fields' => array( 'email', 'email_privacy', 'username', 'password', 'password2', 'phone', 'phone_privacy')
				),
				'personal' => array( 
					'legend' => 'Adauga datele personale', 
					'fields' => array( 'gender', 'last_name', 'first_name', 'birth_date', 'county', 'city', 'address', 'address_privacy', 'specialization', 'grade', 'title')
				),
				'work' => array(
					'legend' => 'Adauga datele locului de munca', 
					'fields' => array( 'job', 'new_job', 'new_job_county', 'new_job_city', 'new_job_address')
				),
				'source_box' => array(
					'legend' => 'Cum ati aflat de noi?',
					'fields' => array( 'source')
				),
				'captcha_box' => array(
					'legend' => '',
					'fields' => array( 'captcha')
				)
			),
			'student' => array(
				'account' => array( 
					'legend' => 'Adauga datele contului', 
					'fields' => array( 'email', 'email_privacy', 'username', 'password', 'password2', 'phone', 'phone_privacy')
				),
				'personal' => array( 
					'legend' => 'Adauga datele personale', 
					'fields' => array( 'gender', 'last_name', 'first_name', 'birth_date', 'county', 'city', 'address', 'address_privacy', 'university', 'college')
				),
				'source_box' => array(
					'legend' => 'Cum ati aflat de noi?',
					'fields' => array( 'source')
				),
				'captcha_box' => array(
					'legend' => '',
					'fields' => array( 'captcha')
				)
			),
			'asistent' => array(
				'account' => array( 
					'legend' => 'Adauga datele contului', 
					'fields' => array( 'email', 'email_privacy', 'username', 'password', 'password2', 'phone', 'phone_privacy')
				),
				'personal' => array( 
					'legend' => 'Adauga datele personale', 
					'fields' => array( 'gender', 'last_name', 'first_name', 'birth_date', 'county', 'city', 'address', 'address_privacy', 'specialization', 'grade')
				),
				'source_box' => array(
					'legend' => 'Cum ati aflat de noi?',
					'fields' => array( 'source')
				),
				'captcha_box' => array(
					'legend' => '',
					'fields' => array( 'captcha')
				)
			),
			'companie' => array(
				'account' => array( 
					'legend' => 'Adauga datele contului', 
					'fields' => array( 'email', 'email_privacy', 'username', 'password', 'password2')
				),
				'personal' => array( 
					'legend' => 'Adauga datele companiei', 
					'fields' => array( 'company_name', 'profile', 'cui', 'reg_no', 'county', 'city', 'address', 'address_privacy', 'contact_person', 'phone', 'phone_privacy', 'fax')
				),
				'source_box' => array(
					'legend' => 'Cum ati aflat de noi?',
					'fields' => array( 'source')
				),
				'captcha_box' => array(
					'legend' => '',
					'fields' => array( 'captcha')
				)
			)
		);
		
		$this->setOptions($_options);
		$this->setIsArray(true);


		$ns_register = new Zend_Session_Namespace('register');

		$this->loadElements($ns_register->type);

		$this->setElementDecorators( array(
			'viewHelper',
			new Application_Form_Decorators_Errors(),
			array(array('data'=>'HtmlTag'),array('tag'=>'td')),
			array('Label',array('tag'=>'td')),
			array(array('row'=>'HtmlTag'),array('tag'=>'tr'))
		));

		$this->captcha->setDecorators(array(
			'Captcha',
			'Description',
			'Errors',
			array(array('dataX'=>'HtmlTag'), array('tag' => 'td')),
			array('Label', array('tag' => 'td')),
			array(array('row2'=>'HtmlTag'),array('tag'=>'tr')),
		));
	}

    public function loadElements( $_form_name ){

		$_properties = Zend_Registry::get('_PROPERTIES');
		foreach ( $this->subForms[$_form_name] as $key => $value ){
			foreach ( $value['fields'] as $field ) {
				switch($field) {
					case 'email':
						$element = $this->CreateElement('text','email');
						$element->setLabel('Email:')
								->setAttrib('id','email')
								->setRequired(true)
								->addValidator('EmailAddress');
						//$element->getDecorator('label')->setOption('requiredSuffix', ' * ');
						$this->addElement($element);
					break;

					case 'email_privacy':
						$element = $this->CreateElement('radio','email_privacy');
						$element->setLabel('')
								->setValue('0')
								->addMultiOptions( array(
									'0' => 'privat',
									'1' => 'public'
								))
								->setSeparator('')
								->setRequired(true);
						$this->addElement($element);
					break;

					case 'username':
						$element = $this->CreateElement('text','username');
						$element->setLabel('Username:')
								->setRequired(true)
								->addValidator('StringLength', false, array(6, 20));
						$this->addElement($element);
					break;

					case 'password':
						$element = $this->CreateElement('password','password');
						$element->setLabel('Parola:')
								->setRequired(true)
								->addValidator('StringLength', false, array(6, 20));
						$this->addElement($element);
					break;

					case 'password2':
						$element = $this->CreateElement('password','password2');
						$element->setLabel('Confirma parola:')
								->setRequired(true)
								->addValidator('StringLength', false, array(6, 20));
						$this->addElement($element);
					break;

					case 'phone':
						$element = $this->CreateElement('text','phone');
						$element->setLabel('Telefon:')
								->setRequired(true)
								->addValidator('StringLength', false, array(9, 20));
						$this->addElement($element);
					break;

					case 'phone_privacy':
						$element = $this->CreateElement('radio','phone_privacy');
						$element->setLabel('')
								->setValue('0')
								->addMultiOptions( array(
									'0' => 'privat',
									'1' => 'public'
								))
								->setSeparator('')
								->setRequired(true);
						$this->addElement($element);
					break;

					case 'gender':
						$element = $this->CreateElement('select','gender');
						$element->setLabel('Gen')
								->setRequired(true)
								->addMultiOptions( array( '' => 'Alege' ) )
								->addMultiOptions( array( $_properties['gender']['male']['value'] => $_properties['gender']['male']['label'] ) )
								->addMultiOptions( array( $_properties['gender']['female']['value'] => $_properties['gender']['female']['label'] ) );
						$this->addElement($element);
					break;

					case 'last_name':
						$element = $this->CreateElement('text','last_name');
						$element->setLabel('Nume:')
								->setRequired(true);
						$this->addElement($element);
					break;

					case 'first_name':
						$element = $this->CreateElement('text','first_name');
						$element->setLabel('Prenume:')
								->setRequired(true);
						$this->addElement($element);
					break;

					case 'birth_date':
						$element = $this->CreateElement('text','birth_date');
						$element->setLabel('Data nasteri:')
								->setAttrib('readonly', true);
						$this->addElement($element);
					break;

					case 'county':
						$modelCounties = new Application_Model_Counties();
						$element = $this->CreateElement('select','county');
						$element->setLabel('Judet:')
								->addMultiOption( '' , 'Alege judet' )
								->addMultiOptions( $modelCounties->getSelect( Application_Model_Countries::DEFAULT_COUNTRY ) )
								->setRequired(true);
						$this->addElement($element);
					break;

					case 'city':
						$modelCities = new Application_Model_Counties();
						$element = $this->CreateElement('select','city');
						$element->setLabel('Localitate:')
								->addMultiOption( '' , 'Alege judet' )
								->setRegisterInArrayValidator(false);
								//->setRequired(true);
						$this->addElement($element);
						$city_selected=$this->CreateElement('hidden','city_selected');
						$this->addElement($city_selected);
					break;

					case 'address':
						$element = $this->CreateElement('textarea','address');
						$element->setLabel('Adresa:')
								->setRequired(true);
						$this->addElement($element);
					break;

					case 'specialization':
						$modelSpecializations = new Application_Model_Specializations();
						$element = $this->CreateElement('select','specialization');
						$element->setLabel('Specializare:')
								->addMultiOption( '' , 'Alege' )
								->addMultiOptions( $modelSpecializations->getSelect() )
								->setRequired(true);
						$this->addElement($element);
					break;

					case 'grade':
						$modelGrades = new Application_Model_Grades();
						$element = $this->CreateElement('select','grade');
						$element->setLabel('Gradul:')
								->addMultiOption( '' , 'Alege' )
								->addMultiOptions( $modelGrades->getSelect() )
								->addMultiOption( '0' , 'Fara grad' )
								->setRequired(true);
						$this->addElement($element);
					break;

					case 'title':
						$modelTitles = new Application_Model_Titles();
						$element = $this->CreateElement('select','title');
						$element->setLabel('Titlu universitar:')
								->addMultiOption( '' , 'Alege' )
								->addMultiOptions( $modelGrades->getSelect() )
								->addMultiOption( '0' , 'Fara titlu' )
								->setRequired(true);
						$this->addElement($element);
					break;

					case 'university':
						$modelUniversities = new Application_Model_Universities();
						$element = $this->CreateElement('select','university');
						$element->setLabel('Universitate:')
								->addMultiOption( '' , 'Alege' )
								->addMultiOptions( $modelUniversities->getSelect() )
								->setRequired(true);
						$this->addElement($element);
					break;

					case 'college':
						$modelColleges = new Application_Model_Colleges();
						$element = $this->CreateElement('select','college');
						$element->setLabel('Facultate:')
								->addMultiOption( '' , 'Alege' )
								->addMultiOptions( $modelColleges->getSelect() )
								->setRequired(true);
						$this->addElement($element);
					break;

					case 'job':
						$modelPharmacies = new Application_Model_Pharmacies();
						$element = $this->CreateElement('select','job');
						$element->setLabel('Locul de munca:')
								->addMultiOption( '' , 'Alege' )
								->addMultiOptions( $modelPharmacies->getSelect() );
								//->setRequired(true);
						$this->addElement($element);
					break;

					case 'new_job':
						$element = $this->CreateElement('text','new_job');
						$element->setLabel('Adauga alt loc de munca:');
								//->setRequired(true);
						$this->addElement($element);
					break;

					case 'new_job_county':
						$modelCounties = new Application_Model_Counties();
						$element = $this->CreateElement('select','new_job_county');
						$element->setLabel('Judet:')
								->addMultiOption( '0' , 'Alege judetu' )
								->addMultiOptions( $modelCounties->getSelect( Application_Model_Countries::DEFAULT_COUNTRY ) );
								//->setRequired(true);
						$this->addElement($element);
					break;

					case 'new_job_city':
						$element = $this->CreateElement('select','new_job_city');
						$element->setLabel('Localitate:')
								->addMultiOption( '0' , 'Alege judet' )
								->setRegisterInArrayValidator(false); //->setRequired(true);
						$this->addElement($element);
						$city_selected=$this->CreateElement('hidden','new_job_city_selected');
						$this->addElement($city_selected);
					break;

					case 'new_job_address':
						$element = $this->CreateElement('textarea','new_job_address');
						$element->setLabel('Adresa:');
								//->setRequired(true);
						$this->addElement($element);
					break;

					case 'company_name':
						$element = $this->CreateElement('text','company_name');
						$element->setLabel('Nume companie:')
								->setRequired(true);
						$this->addElement($element);
					break;

					case 'cui':
						$element = $this->CreateElement('text','cui');
						$element->setLabel('CUI:')
								->setRequired(true);
						$this->addElement($element);
					break;

					case 'reg_no':
						$element = $this->CreateElement('text','reg_no');
						$element->setLabel('Nr. inregistrare(J):')
								->setRequired(true);
						$this->addElement($element);
					break;

					case 'fax':
						$element = $this->CreateElement('text','fax');
						$element->setLabel('Fax:');
						$this->addElement($element);
					break;

					case 'source':
						$element = $this->CreateElement('select','source');
						$element->setLabel('Cum ati aflat de noi:')
								->addMultiOption( '0' , 'Alege' );
						foreach( $_properties['source'] as $source_val ){
							$element->addMultiOptions( array( $source_val => $source_val ) );
						}
						$this->addElement($element);
					break;

					case 'captcha':
						//$recaptcha = new Zend_Service_ReCaptcha($public, $private);

						$captcha = new Zend_Form_Element_Captcha('captcha', array(
							'label' => 'Write the chars to the field',
							'captcha' => array( 
								'captcha' => 'Image',
								'wordLen' => 1,
								'timeout' => 300,
								'font' => './media/images/captcha/verdana.ttf',
								'imgDir' => './media/images/captcha/',
								'imgUrl' => '/media/images/captcha/',
								//'service' => $recaptcha
							)
						));
						$this->addElement($captcha);
					break;
				}
			}

			// set group
			$this->addDisplayGroup( $value['fields'] , $key , array('legend' => $value['legend']) );
			$$key = $this->getDisplayGroup($key);
			$$key->setDecorators(array(
					'FormElements',
					array('Fieldset', array('class'=>'heading')),
					array('HtmlTag', array('tag'=>'table','class'=>'form_1')),
			));
		}
    }
}
